package com.weavernorth.util;

import com.weavernorth.create.GetToken;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

public class CallBack {
    private static Log log = org.apache.commons.logging.LogFactory.getLog(CallBack.class.getName());

    public static void callBack(StringBuffer applyids) {
        BaseBean bb = new BaseBean();
        JSONObject jsonObjects = new JSONObject();
        //存放所有的applyid


        //调用回调接口，标记已读
        RecordSet rs = new RecordSet();
        RecordSet rsTwo = new RecordSet();
        RecordSet rsThree = new RecordSet();
        RecordSet rsStr = new RecordSet();


        //调用接口需要的参数
        //获取回调接口
        String strBackUrl = Util.null2String(bb.getPropValue("porturl", "backurl"));
       log.info("调用的回调的接口地址：" + strBackUrl);
        String strCode = "UTF-8";
        //type类型
        String strType = Util.null2String(bb.getPropValue("porturl","type"));
        //拼接表中已有返回状态为1的简历id
        StringBuffer applyidsTwo = new StringBuffer();
        //查询返回状态的sql
        String strSql = "select lcid  from uf_hd where fhzt='1'";
        rsStr.executeQuery(strSql);
        //判断表中是否有已经推送失败的数据
        if (rsStr.next()) {
            //查询返回状态的sql：
            String strSqlTwo = "select lcid  from uf_hd where fhzt='1'";
            log.info("查询返回状态为1的sql：" + strSqlTwo);
            rs.execute(strSqlTwo);
            //首先将返回状态失败的数据推送
            while (rs.next()) {
                applyidsTwo.append(rs.getString("lcid") + ",");
               log.info("aaa" + applyidsTwo);
            }
            String substringTwo = applyidsTwo.substring(0, applyidsTwo.length() - 1);
            log.info("拼接后并去掉最后逗号的返回状态为1的简历id" + substringTwo);
            JSONObject js = new JSONObject();
            js.put("applyIds", substringTwo);

            js.put("cType", strType);
            //获取token
            GetToken getToken = new GetToken();
            String strToken = Util.null2String(getToken.getToken());
            log.info("已经获取到的token2：" + strToken);
            //调用接口
            log.info("推送的数据:"+js.toString());
            String resultStrTwo = GetToken.sendPost(strBackUrl, js, strCode, strToken);
            JSONObject jsbTwo = JSONObject.fromObject(resultStrTwo);
            //返回状态为0代表推送成功
            String satutsTwo = Util.null2String((String) jsbTwo.get("state"));
            if ("0".equals(satutsTwo)) {
                //修改表中数据
                String sqlTwo = "update uf_hd set fhzt = '0' ";
                log.info("修改返回状态：" + sqlTwo);
                rsTwo.executeUpdate(sqlTwo);
            }
        }

        //传入的参数
        //去掉最后一个逗号
        String strSubstring = applyids.substring(0, applyids.length() - 1);
        log.info("传入的applyids：" + strSubstring);
        jsonObjects.put("applyIds", strSubstring);
        jsonObjects.put("cType", strType);
        //jsonObjects.put("cType","1");
        log.info("传入的json：" + jsonObjects);
        try {
            //调用接口
            log.info("aaaaaaaaaaaaaaaaa");
            //获取token值
            GetToken getToken = new GetToken();
            String strTokenTwo = Util.null2String(getToken.getToken());
            log.info("已经获取到的token3：" + strTokenTwo);

            String resultStr = GetToken.sendPost(strBackUrl, jsonObjects, strCode, strTokenTwo);
            log.info("resultStr:aaaa" + resultStr);
            JSONObject jsb = JSONObject.fromObject(resultStr);
            //从返回值获取状态
            String strSatuts = Util.null2String(jsb.getString("state"));
            log.info("返回值状态：" + strSatuts);
            if ("0".equals(strSatuts)) {
                //修改表中数据
                String sqlTwo = "update uf_hd set fhzt = '0' ";
                rsTwo.executeUpdate(sqlTwo);
            } else {
                String sqlThree = "update uf_hd set fhzt = '1' ";
                rsThree.executeUpdate(sqlThree);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
